/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.usc.cs576.lib.mediaObject;

import edu.usc.cs576.lib.Action;
import edu.usc.cs576.lib.MediaObject;
import java.awt.Point;
import java.util.ArrayList;
import javax.swing.JLabel;

/**
 *
 * @author haoxu
 */
public class Track implements MediaObject {

    private String name;
    private ArrayList<Action> actions;
    private ArrayList<KeyFrame> keyframes;
    private int width;
    private int height;
    int startFrame;
    int endFrame;

    public Track() {
        keyframes = new ArrayList<KeyFrame>();
    }

    public void load() {
    }

    public void show() {
    }

    public ArrayList<Action> getActions() {
        if (actions == null) {
            actions = new ArrayList<Action>();
        }
        return actions;
    }

    public void setActions(ArrayList<Action> actions) {
        this.actions = actions;
    }

    public String getName() {
        return name;
    }

    public ArrayList<KeyFrame> getKeyframes() {
        if (keyframes == null) {
            keyframes = new ArrayList<KeyFrame>();
        }
        return keyframes;
    }

    public void setKeyframes(ArrayList<KeyFrame> keyframes) {
        this.keyframes = keyframes;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void addKeyFrame(KeyFrame keyframe) {
        if (keyframes.isEmpty()) {
            keyframes.add(keyframe);
        }

        if (keyframes.get(keyframes.size() - 1).frameNum < keyframe.frameNum) {
            keyframes.add(keyframe);
        } else {
            for (int i = 0; i < keyframes.size(); i++) {
                if (keyframes.get(i).frameNum == keyframe.frameNum) {
                    keyframes.set(i, keyframe);
                    break;
                } else if (keyframes.get(i).frameNum > keyframe.frameNum) {
                    keyframes.add(i, keyframe);
                    break;
                }
            }
        }
    }

    public Point getLocation(int frame) {
        Point point = new Point();

        if (keyframes.isEmpty()) {
            return null;
        }

        if (keyframes.get(0).frameNum > frame) {
            return null;
        }

        if (keyframes.get(keyframes.size() - 1).frameNum < frame) {
            return null;
        }

        KeyFrame frame1 = null;
        KeyFrame frame2 = null;
        for (int i = 0; i < keyframes.size(); i++) {
            if (keyframes.get(i).frameNum == frame) {
                return new Point(keyframes.get(i).position_x, keyframes.get(i).position_y);
            }
            if (keyframes.get(i).frameNum > frame) {
                frame1 = keyframes.get(i - 1);
                frame2 = keyframes.get(i);
            }
        }

        double ratio = (double) (frame - frame1.frameNum) / (double) (frame2.frameNum - frame1.frameNum);

        int x = (int) ((double) (frame1.position_x) + (double) (frame2.position_x - frame1.position_x) * ratio);
        int y = (int) ((double) (frame1.position_y) + (double) (frame2.position_y - frame1.position_y) * ratio);

        point.x = x;
        point.y = y;

        return point;
    }

    public void setStartFrame(int frame) {
        this.startFrame = frame;
    }

    public int getStartFrame() {
        return this.startFrame;
    }

    public void setEndFrame(int frame) {
        this.endFrame = frame;
    }

    public int getEndFrame() {
        return this.endFrame;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("Track{" + "name=" + name + "width=" + width + "height=" + height + "startFrame=" + startFrame + "endFrame=" + endFrame + "}");
        sb.append("\nkeyframes[");
        for (int i = 0; i < keyframes.size(); i++) {
            sb.append(keyframes.get(i));
        }
        sb.append("]\n");
        return sb.toString();

    }

    public void setName(String name) {
        this.name=name;
    }
}
